package com.mf.service.impl;

import com.mf.entity.Order;
import com.mf.repository.OrderRepository;
import com.mf.service.OrderService;
import com.mf.util.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * order实现类
 *
 * @author: Jiang Runqing
 * @date: 2018-09-10 21:18
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderRepository orderRepository;

    @Override
    public List<Order> list(Order order, Integer page, Integer pageSize, Sort.Direction direction, String... properties) {
        Pageable pageable = new PageRequest(page - 1, pageSize);
        Page<Order> pageOrder = orderRepository.findAll(new Specification<Order>() {
            @Override
            public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                queryCondition(predicate, order, root, cb);
                return predicate;
            }
        }, pageable);
        return pageOrder.getContent();
    }

    @Override
    public Long getCount(Order order) {
        Long count = orderRepository.count(new Specification<Order>() {
            @Override
            public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                queryCondition(predicate, order, root, cb);
                return predicate;
            }
        });
        return count;
    }

    private long string2Number(String str) {
        long result = 0;
        try {
            result = Long.parseLong(str);
        } catch (NumberFormatException e) {
            System.out.println("String: " + str + "转换异常。" + e.getStackTrace() + e.getMessage());
        } finally {
        }
        return result;
    }

    /**
     * list方法查询的条件拼接
     *
     * @param predicate
     * @param order
     * @param root
     * @param cb
     */
    private void queryCondition(Predicate predicate, Order order, Root<Order> root, CriteriaBuilder cb) {

        if (order != null) {
            // 订单号
            if (StringUtil.isNotEmpty(order.getOrderSN())) {
                predicate.getExpressions().add(cb.like(root.get("orderSN"), "%" + order.getOrderSN() + "%"));
            }
            //userId
            if (null != order.getUserId()) {
                predicate.getExpressions().add(cb.equal(root.get("userId"), order.getUserId()));
            }
            //订单状态 orderStatus
            if (null != order.getOrderStatus()) {
                predicate.getExpressions().add(cb.equal(root.get("orderStatus"), order.getOrderStatus()));
            }
            //订单时间 大于
            if (null != order.getAddTimeStart()) {
                predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("addTime"), order.getAddTimeStart()));
            }
            //订单时间   小于
            if (null != order.getAddTimeEnd()) {
                predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("addTime"), order.getAddTimeEnd()));
            }

        }

    }

    @Override
    public void save(Order order) {
        orderRepository.save(order);
    }

    @Override
    public Order getById(int id) {
        return orderRepository.findOne(id);
    }

    @Override
    public void updateStatus(Order order) {
        orderRepository.updateStatus(order.getOrderId(), order.getOrderStatus());
    }

}
